package org.kabeja.parser;

import org.kabeja.dxf.DXFDocument;
import org.kabeja.parser.entities.*;
import org.kabeja.parser.objects.*;
import org.kabeja.parser.table.*;
import org.kabeja.tools.FileEncodeUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author shutao
 * @since 2022/4/25 14:30
 */
public class ParserBuilder {

    public static DXFDocument getDocument(String dxfFilePath) throws FileNotFoundException, ParseException{
        // 读取文件
        File file = new File(dxfFilePath);
        return getDocument(file);
    }

    public static DXFDocument getDocument(File file) throws FileNotFoundException, ParseException {
        InputStream inputStream = new FileInputStream(file);

        // 解析图元
        Parser dxfParser = createDefaultParser();
        dxfParser.parse(inputStream, FileEncodeUtil.getJavaEncode(file));
        return dxfParser.getDocument();
    }

    /**
     * 创建 dxf 处理程序
     * @author shutao
     * @date 2022/4/25 16:20
     * @return 处理程序
     */
    private static Parser createDefaultParser() {
        // 创建 dxf 文件解析处理器
        DXFParser dxfParser = new DXFParser();

        // headers
        dxfParser.addHandler(new DXFHeaderSectionHandler());

        // blocks
        DXFBlocksSectionHandler blocksHandler = new DXFBlocksSectionHandler();
        // 添加图形处理器
        addEntityHander(blocksHandler);
        // 添加到主处理器
        dxfParser.addHandler(blocksHandler);

        // entities
        DXFEntitiesSectionHandler entitiesHandler = new DXFEntitiesSectionHandler();
        // 添加图元处理器
        addEntityHander(entitiesHandler);
        // 添加到主处理器
        dxfParser.addHandler(entitiesHandler);

        // tables
        DXFTableSectionHandler tablesHandler = new DXFTableSectionHandler();
        tablesHandler.addHandler(new DXFLayerTableHandler());
        tablesHandler.addHandler(new DXFLineTypeTableHandler());
        tablesHandler.addHandler(new DXFDimensionStyleTableHandler());
        tablesHandler.addHandler(new DXFStyleTableHandler());
        tablesHandler.addHandler(new DXFVPortTableHandler());
        tablesHandler.addHandler(new DXFViewTableHandler());
        // 添加到主处理器
        dxfParser.addHandler(tablesHandler);

        // objects
        DXFObjectsSectionHandler objectsHandler = new DXFObjectsSectionHandler();
        objectsHandler.addHandler(new DXFDictionaryHandler());
        objectsHandler.addHandler(new DXFPlotsettingsHandler());
        objectsHandler.addHandler(new DXFLayoutHandler());
        objectsHandler.addHandler(new DXFMLineStyleHandler());
        objectsHandler.addHandler(new DXFSpatialFilterHandler());
        objectsHandler.addHandler(new DXFXRecordHandler());
        // 添加到主处理器
        dxfParser.addHandler(objectsHandler);
        
        return dxfParser;
    }

    /**
     * 图形处理程序
     * @author shutao
     * @date 2022/4/25 16:21
     * @param handler 处理程序
     */
    private static void addEntityHander(DXFEntitiesSectionHandler handler) {
        // 公有处理器
        handler.addHandler(new DXFLineHandler());
        handler.addHandler(new DXFCircleHandler());
        handler.addHandler(new DXFArcHandler());
        handler.addHandler(new DXFPolylineHandler());
        handler.addHandler(new DXFLWPolylineHandler());
        handler.addHandler(new DXFMTextHandler());
        handler.addHandler(new DXFTextHandler());
        handler.addHandler(new DXFInsertHandler());
        handler.addHandler(new DXFEllipseHandler());
        handler.addHandler(new DXFSolidHandler());
        handler.addHandler(new DXFHatchHandler());
        handler.addHandler(new DXFRayHandler());
        handler.addHandler(new DXFRegionHandler());
        handler.addHandler(new DXFSplineHandler());
        handler.addHandler(new DXFMLineHandler());
        handler.addHandler(new DXFLeaderHandler());
        handler.addHandler(new DXFViewportHandler());
        handler.addHandler(new DXFDimensionHandler());
    }
}
